package cn.com.nes.site.service.system.dict.impl;

import cn.com.nes.common.util.CopyUtils;
import cn.com.nes.mybatis.agent.ope.entity.IsDictValueInfo;
import cn.com.nes.mybatis.agent.ope.mapper.IsDictValueInfoMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.system.dict.IsDictValueInfoService;
import cn.com.nes.site.service.system.dict.dto.IsDictValueInfoDTO;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ShenWei
 * @Date: 2024/04/01/9:51
 */
@Service
public class IsDictValueInfoServiceImpl extends ServiceImpl<IsDictValueInfoMapper, IsDictValueInfo> implements IsDictValueInfoService {
    @Resource
    IsDictValueInfoMapper isDictValueInfoMapper;

    @Override
    public PageData getDictValue(String manufactureId, String typeId) {
        List<IsDictValueInfoDTO> list = getChildList(manufactureId,typeId,null);

        HashMap<String, Object> map = new HashMap<>();
        map.put("dictTree",list);
        return PageData.ok(map);
    }

    // 递归查询子列表
    private List<IsDictValueInfoDTO> getChildList(String manufactureId, String typeId, String parentId){
        // 查询根节点列表
        QueryWrapper<IsDictValueInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",typeId);
        wrapper.eq("manufacture_id",manufactureId);
        wrapper.eq("sfsc",0);
        if (StrUtil.isBlank(parentId)){
            wrapper.isNull("p_value_id");
        } else {
            wrapper.eq("p_value_id",parentId);
        }
        wrapper.orderByDesc("cjsj");
        List<IsDictValueInfo> list = this.list(wrapper);

        List<IsDictValueInfoDTO> dtoList = CopyUtils.copyList(list,IsDictValueInfoDTO.class);
        // 递归查询子列表
        for (IsDictValueInfoDTO node : dtoList) {
            List<IsDictValueInfoDTO> childList = getChildList(manufactureId,typeId,node.getValueId());
            node.setChildList(childList);
        }

        return dtoList;
    }
}
